package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MDAccountExcution;
import com.fortune.dto.in.mDAccountMgt.MDAccountListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MDAccountEnum;
import com.fortune.service.MDRelationService;
import com.fortune.vo.mDAccountMgt.MDealerInfo;
import com.fortune.vo.mDAccountMgt.MDealerList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "mDAccountMgt", description = "厂商经销商管理", tags = {"厂商经销商管理"})
@RestController
@RequestMapping("mDAccountMgt")
public class MDAccountMgtController {
    @Autowired
    private MDRelationService mdRelationService;

    @ApiOperation(value = "经销商列表", nickname = "accountList", notes = "", tags={ "@石匡" })
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
            @ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
            @ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
    })
    @GetMapping(value = "accountList",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData<MDealerList>> list(HttpServletRequest request, @ApiIgnore @Validated MDAccountListDto mdAccountListDto) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        mdAccountListDto.setPage((mdAccountListDto.getPage()-1)*10);
        List<MDealerList> list = mdRelationService.queryDelareList(companyId,mdAccountListDto);
        int count = mdRelationService.queryDelareListCount(companyId,mdAccountListDto);
        PagingData<MDealerList> pagingData = new PagingData<>();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new MDAccountExcution<PagingData<MDealerList>>(pagingData, MDAccountEnum.SUCCESS);
    }

    @ApiOperation(value = "经销商详情", nickname = "dealerInfo", notes = "", tags={ "@石匡" })
    @ApiImplicitParam(name="phone", value="手机号",paramType="query",dataType="int")
    @GetMapping(value = "list",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<MDealerInfo> dealerInfo(@Validated @NotBlank(message="手机号不能为空")String phone) {
        MDealerInfo info = mdRelationService.queryDealerInfo(phone);
        if (info == null){
            return new MDAccountExcution<MDealerInfo>(null, MDAccountEnum.NO_EXIST);
        }
        return new MDAccountExcution<MDealerInfo>(info, MDAccountEnum.SUCCESS);
    }

    @ApiOperation(value = "添加经销商", nickname = "dealerAdd", notes = "@石匡")
    @ApiImplicitParam(name="id", value="经销商id",paramType="query",dataType="int")
    @PostMapping(value = "add",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @NotBlank(message="经销商id不能为空")String id) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        int num = mdRelationService.queryRelationIsExist(companyId,id);
        if (num > 0){
            return new MDAccountExcution<Void>(null, MDAccountEnum.EXIST);
        }
        int max = mdRelationService.queryCompanyDealerMaxCount(companyId);
        int count = mdRelationService.queryDelareOfCompanyCount(companyId);
        if (count >= max){
            return new MDAccountExcution<Void>(null, MDAccountEnum.MAX);
        }
        num = mdRelationService.addRelationIs(companyId,id);
        if (num < 1){
            return new MDAccountExcution<Void>(null, MDAccountEnum.FALSE);
        }
        return new MDAccountExcution<Void>(null, MDAccountEnum.SUCCESS);
    }


    @ApiOperation(value = "删除经销商", nickname = "dealerDelete", notes = "@石匡")
    @ApiImplicitParam(name="id", value="经销商id",paramType="query",dataType="string")
    @DeleteMapping(value = "delete",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="经销商id不能为空") String id) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        int result = mdRelationService.delete(companyId,id);
        if(result < 1){
            return new MDAccountExcution<Void>(null, MDAccountEnum.FALSE);
        }
        return new MDAccountExcution<Void>(null, MDAccountEnum.SUCCESS);

    }

}
